package server

import (
	"context"
	"video-mgr/video-server/basic/config"
	"video-mgr/video-server/basic/handler/model"
	"video-mgr/video-server/basic/proto"
)

type Server struct {
	__.UnimplementedVideoServer
}

func (s Server) ListVideo(_ context.Context, in *__.ListVideoReq) (*__.ListVideoResp, error) {
	//TODO implement me

	offset := (in.Page - 1) * in.Size
	var videoList []*__.ListVideo
	for _, req := range videoList {

		list := &__.ListVideo{
			Title:   req.Title,
			Content: req.Content,
			Sort:    req.Sort,
			Address: req.Address,
			Year:    req.Year,
			Image:   req.Image,
		}
		videoList = append(videoList, list)
	}
	err := config.DB.Offset(int(offset)).Limit(int(in.Size)).Error
	if err != nil {
		return &__.ListVideoResp{
			ListVideo: nil,
		}, nil
	}
	return &__.ListVideoResp{ListVideo: videoList}, nil

}

func (s Server) CreateReview(_ context.Context, in *__.CreateReviewReq) (*__.CreateReviewResp, error) {
	//TODO implement me

	var review model.Review
	review = model.Review{
		UserID:  int(in.UserID),
		VideoID: int(in.VideoID),
		Status:  in.Status,
	}
	err := model.CreateReview(&review)
	if err != nil {
		return &__.CreateReviewResp{
			Code: 0,
			Msg:  "审核失败",
		}, nil
	}
	return &__.CreateReviewResp{
		Code: 200,
		Msg:  "审核成功",
	}, nil
}

func (s Server) CreateVideo(_ context.Context, in *__.CreateVideoReq) (*__.CreateVideoResp, error) {
	//TODO implement me

	var video model.Video
	video = model.Video{
		Title:   in.Title,
		Content: in.Content,
		Sort:    in.Sort,
		Address: in.Address,
		Year:    in.Year,
		Image:   in.Image,
	}
	err := model.CreateVideo(&video)
	if err != nil {
		return &__.CreateVideoResp{
			Code: 0,
			Msg:  "视频发布失败",
		}, nil
	}
	return &__.CreateVideoResp{
		Code: 200,
		Msg:  "视频发布成功",
	}, nil

}

func (s Server) CreateUser(_ context.Context, in *__.CreateUserReq) (*__.CreateUserResp, error) {
	//TODO implement me
	var user model.User
	user = model.User{
		Username: in.Name,
		Password: in.Password,
	}
	err := model.CreateUser(&user)
	if err != nil {
		return &__.CreateUserResp{
			Code: 0,
			Msg:  "登录失败",
		}, nil
	}
	return &__.CreateUserResp{
		Code: 200,
		Msg:  "登录成功",
	}, nil
}
